package com.cmc.cloud.cmclink.doc.controller.admin.special;

import com.alibaba.excel.EasyExcel;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoCasConvert;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoCasDO;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoCasService;
import com.cmc.cloud.cmclink.doc.vo.specialcargocasvo.*;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.excel.core.util.ExcelUtils;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


/**
 * 特种货申请dg下cas信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 特种货申请dg下cas信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/specialCargoCas")
public class SpecialCargoCasController {

    @Resource
    private SpecialCargoCasService specialCargoCasService;

    @Operation(summary = "创建特种货申请dg下cas信息")
    @PostMapping("/create")
    public CommonResult<Void> createSpecialCargoCas(@Valid @RequestBody SpecialCargoCasBaseVO createReqVO) {
        specialCargoCasService.createSpecialCargoCas(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新特种货申请dg下cas信息")
    @PostMapping("/update")
    public CommonResult<Void> updateSpecialCargoCas(@Valid @RequestBody SpecialCargoCasUpdateReqVO updateReqVO) {
        specialCargoCasService.updateSpecialCargoCas(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除特种货申请dg下cas信息")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteSpecialCargoCas(@PathVariable("id") Long id) {
        specialCargoCasService.deleteSpecialCargoCas(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得特种货申请dg下cas信息")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SpecialCargoCasRespVO> getSpecialCargoCas(@PathVariable("id") Long id) {
        SpecialCargoCasDO specialCargoCas = specialCargoCasService.getSpecialCargoCas(id);
        return CommonResult.success(SpecialCargoCasConvert.INSTANCE.convert(specialCargoCas));
    }

    @Operation(summary = "获得特种货申请dg下cas信息列表")
    @GetMapping("/list")
    @Parameter(name = "dgId", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<SpecialCargoCasRespVO>> getSpecialCargoCasList(@RequestParam("dgId") Long dgId) {
        List<SpecialCargoCasDO> list = specialCargoCasService.getSpecialCargoCasList(dgId);
        return CommonResult.success(SpecialCargoCasConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "casNo检查")
    @PostMapping("/checkCasNo")
    @Parameter(name = "casNos", description = "CAS编号集合", required = true)
    public CommonResult<Void> checkCasNo(@RequestBody List<String> casNos) {
        specialCargoCasService.checkCasNo(casNos);
        return CommonResult.success();
    }

    @GetMapping("/excel-export")
    @Operation(summary = "excel导出")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<CasImportTemplateVO> exportList = new ArrayList<>();
        response.addHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode("cas_import_template.xlsx", StandardCharsets.UTF_8.name()));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ExcelUtils.write(response, "cas_import_template.xls", "CAS", CasImportTemplateVO.class, exportList);
    }

    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "excel新增导入")
    public CommonResult<List<CasImportTemplateVO>> excelCreateImport(@RequestPart @RequestParam(value = "file") MultipartFile file) throws IOException {
        List<CasImportTemplateVO> casImportData = EasyExcel.read(file.getInputStream()).head(CasImportTemplateVO.class).autoCloseStream(true).sheet().doReadSync();
        return CommonResult.success(casImportData);
    }

    @Operation(summary = "获得特种货申请dg下cas信息分页")
    @PostMapping("/page")
    public CommonResult<PageResult<SpecialCargoCasRespVO>> getSpecialCargoCasPage(@RequestBody @Valid SpecialCargoCasPageReqVO pageVO) {
        PageResult<SpecialCargoCasDO> pageResult = specialCargoCasService.getSpecialCargoCasPage(pageVO);
        List<SpecialCargoCasRespVO> results = SpecialCargoCasConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
